﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using PiscoSourDominio;
using PiscoSourSOAPRestServices.Persistencia;
using Dapper;

namespace PiscoSourPersistencia
{
    public class OrdenDAO
    {
        public string InsertarOrdenes(Orden orden)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "INSERT INTO PSR_ORDEN(intCantidad,vchObservacion,intCodPlato,intReserva) VALUES (@intCantidad,@vchObservacion,@intCodPlato,@intReserva)";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCantidad = orden.intCantidad,
                            vchObservacion = orden.vchObservacion,
                            intCodPlato = orden.intCodPlato,
                            intReserva = orden.intReserva
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string ActualizarOrdenes(Orden orden)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "UPDATE PSR_ORDEN SET intCantidad=@intCantidad, vchObservacion=@vchObservacion, intCodPlato=@intCodPlato, intReserva=@intReserva WHERE intOrden=@intOrden";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intOrden = orden.intOrden,
                            intCantidad = orden.intCantidad,
                            vchObservacion = orden.vchObservacion,
                            intCodPlato = orden.intCodPlato,
                            intReserva = orden.intReserva
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string EliminarOrdenes(Orden orden)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "DELETE FROM PSR_ORDEN WHERE intOrden=@intOrden";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intOrden = orden.intOrden
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        
        public Orden ObtenerOrden(string codigo)
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                const string sqlQuery = "select * from PSR_ORDEN where intorden=@intorden";
                var orden = sqlConnection.Query<Orden>(sqlQuery, new { intorden = codigo }).FirstOrDefault<Orden>();

                sqlConnection.Close();

                return orden;
            }
        }

        public IEnumerable<Orden> ListarOrdenes()
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                var sqlQuery = string.Format(@"Select * from PSR_ORDEN");
                var result = sqlConnection.Query<Orden>(sqlQuery);
                sqlConnection.Close();
                return result;
            }
        }
    }
}